package org.jmresler.sakila.batch.services;

import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;

@Slf4j
@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Value("${spring.mail.recipients}")
    private String recipients;
    @Value("${spring.mail.subject}")
    private String subject;
    @Value("${spring.mail.text}")
    private String text;

    public void sendMessageWithAttachment(final String pathToAttachment) throws MessagingException
    {
        var mimeMessage = mailSender.createMimeMessage();
        var file = new File(pathToAttachment);
        var helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

        helper.setFrom(from);
        helper.setTo(recipients);
        helper.setSubject(subject);
        helper.setText(text);
        helper.addAttachment("Billing-Process-Report.html", new FileSystemResource(file));

        mailSender.send(mimeMessage);
    }
}
